package com.platform.controller;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.platform.service.LotteryWagerService;
import com.platform.utils.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.platform.entity.LotteryWinningEntity;
import com.platform.service.LotteryWinningService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;

/**
 * 开奖记录表Controller
 *
 * @author liweihai
 * @email liweihai12345@sina.com
 * @date 2019-05-05 14:01:12
 */
@RestController
@RequestMapping("lotterywinning")
public class LotteryWinningController {
    @Autowired
    private LotteryWinningService lotteryWinningService;
    @Autowired
    private LotteryWagerService lotteryWagerService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("lotterywinning:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<LotteryWinningEntity> lotteryWinningList = lotteryWinningService.queryList(query);
        int total = lotteryWinningService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(lotteryWinningList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("lotterywinning:info")
    public R info(@PathVariable("id") Integer id) {
        LotteryWinningEntity lotteryWinning = lotteryWinningService.queryObject(id);

        return R.ok().put("lotteryWinning", lotteryWinning);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("lotterywinning:save")
    public R save(@RequestBody LotteryWinningEntity lotteryWinning) {
        lotteryWinningService.save(lotteryWinning);

        return R.ok();
    }

    /**
     * 开奖
     */
    @RequestMapping("/openlotter")
    @RequiresPermissions("lotterywinning:update")
    public R openlotter(@RequestBody LotteryWinningEntity lotteryWinning) {
        //修改本期状态
        lotteryWinning.setStatus(1);
        int count = lotteryWinningService.update(lotteryWinning);
        if (count > 0) {
            //保存下期
            LotteryWinningEntity newlottery = new LotteryWinningEntity();
            Integer stages = Integer.parseInt(lotteryWinning.getStages()) + 1;
            newlottery.setStages(stages + "");
            newlottery.setStatus(0);
            newlottery.setType(5);
            newlottery.setOpenDate(lotteryWinning.getOpenDate());
            newlottery.setText(lotteryWinning.getText());
            lotteryWinningService.save(newlottery);
            lotteryWagerService.openlottery(lotteryWinning);
            return R.ok();
        }else
            return R.error("开奖失败请稍后重试！");
    }

    public static Integer getWeekOfDate(Date dt) {
        Integer[] weekDays = {7, 1, 2, 3, 4, 5, 6};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;

        return weekDays[w];
    }
    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("lotterywinning:update")
    public R update(@RequestBody LotteryWinningEntity lotteryWinning) {
        lotteryWinningService.update(lotteryWinning);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("lotterywinning:delete")
    public R delete(@RequestBody Integer[] ids) {
        lotteryWinningService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<LotteryWinningEntity> list = lotteryWinningService.queryList(params);

        return R.ok().put("list", list);
    }
}
